from collections import namedtuple  # 导入命名元组模块
from openpyxl import load_workbook
from Tools.read_config import ReadConfig
from Tools import get_path
from Tools.Get_Cookies import GetData

a = [12, "123"]
print(type(a))

a.append(1)
print(a)

# 生成从1到100的的列表几种方法
# 使用列表推导式
list2 = [i for i in range(1, 101)]
print('列表推导式的值为：', list2)
# 直接转化list
list3 = list(range(1, 101))
print(list3)
# 复杂运算平方数
squares = [i ** 2 for i in range(1, 101)]  # 1-100的平方[5,6](@ref)
print(f"这是生成100以内数的平方：{squares}")

# 带步长生成奇数
odd_list = [i for i in range(1, 101) if i % 2 != 0]  # 1-100的奇数[3,7](@ref)
print(f"这是生成100以内的奇数：{odd_list}")

# 带步长生成偶数
origin_list = [i for i in range(1, 101) if i % 2 != 1]
print(f"这是生成100以内的偶数：{origin_list}")

# for循环实现
for_list = []
for i in range(1, 101):
    for_list.append(i)
print()

# while循环实现
i = 1
while_list = []
while i < 101:
    while_list.append(i)
    i += 1
print(f"这是while循环实现的：{while_list}")

# 实例化命名元组
student = namedtuple('student', ['name', 'age', 'gender'])
# 赋值
s = student('小明', 18, '男')
print(s)

url = [f"page{i}" for i in range(1, 101)]
print(f"推导式结合format格式化的值：{url}")

# 字典结合推导式
dict = {i: i + 1 for i in range(10)}
print(dict)

# 练习题1
# 生成1，5，10的列表，通过for循环
four_list = [i for i in range(0, 51, 5)]
print(f"for循环生成50以内的能被5整除的值：{four_list}")

if_list = [i for i in range(0, 51) if i % 5 == 1]
print(if_list)

# 练习题2
#
page_list = [f"paage{i}" for i in range(1, 11)]
print(f"推导式生成page:{page_list}")
# 使用enumerate函数
li = ['a', 'b', 'c', 'd']

dict_a = {}
for i, j in enumerate(li):
    dict_a[i] = j
print(f"快速将列表转换为字典，输出字典的键和值：{dict_a}")

cook_str = 'BAIDUPSID=ADSBADSADADADS;PSTM=13213213132;baiduid=13213818328131;sugstore=0l;speed=BSAODSADSA'
dict_aa = {
    **{i: j for segment in cook_str.split(';') if '=' in segment for i, j in [segment.split('=', 1)]},
    **{i: j for segment in cook_str.split(';') if ':' in segment for i, j in [segment.split(':', 1)]}
}
print(cook_str)
# test_cook_dict = {i.split('=')[0]:i.split('=')[1]}
# print(test_cook_dict)
cook_dict = {i.split('=')[0]: i.split('=')[1] for i in cook_str.split(';')}
print(f"这是cook_dict:{cook_dict}")

# 使用小括号的是生成器表达式
# for j in range(100):
gen_ = (i for i in range(10))  # 生成器智慧
print(gen_)
B_gen_ = next(gen_)
print(next(gen_))


def gen_func():
    yield 100
    print('hello python')
    yield 1000
    print('hello java')
    yield 10000
    print('hello ruby')


res = gen_func()
print(f"123:", (next(res)))
print(f"1234:", (next(res)))

# 生成器和迭代器的区别
# 可以for循环的都是可迭代对象，内部只实现iter()方法
li = [1, 2, 3, 4]

li1 = iter(li)  # 实现列表内部的生成器Iter()方法
print(iter(li1))  # 调用iter()

print(next(li1))  # 在__iter__方法之外还实现了__next__# 方法
print(type(li1))
print(type(cook_str))

cc = (1, 2, 3,)
print(type(cc))
# gen_func().send()
# gen_func().throw()
# gen_func().close()

_gen = (x ** 2 for x in range(5))
next(gen_)
print(_gen)
print(list(_gen))


# _gen.close()
# _gen.send(None)

def output_next():
    for i in range(1, 5):
        se = yield i
        print(f"se的值是：{se}")


g = output_next()
print(next(g))

print(g.send(100))
print(next(g))

xx = {i for i in range(1, 100)}
print(xx)

ee = [i for i in range(2, 100) if i % 2 != 0]
print(ee)


def excel_read(file_path, sheet_name):
    try:
        mode = eval(ReadConfig.get_config(get_path.case_config_path, 'MODE', 'mode'))
        # 加载文件薄
        wb = load_workbook(file_path)
        # 加载工作表
        ws = wb[sheet_name]

        col = ws.max_column +1
        row = ws.max_row+1
        test_data = ({ws.cell(1, j).value: ws.cell(i, j).value for j in range(1, col)} for i in range(2, row))
        # for i in test_data:
        #     print(next(test_data))
        test_data1 = {
            j: [ws.cell(i, j).value for i in range(2, row)]
            for j in range(1, col)
        }

        test_data2 = [
            [(ws.cell(1, j).value, ws.cell(i, j).value) for j in range(1, col) if ws.cell(i, j).value is not None]
            for i in range(2, row)
        ]

        test_data3 = [
            {ws.cell(1, j).value: ws.cell(i, j).value for j in range(1, col) if ws.cell(i, j).value is not None}
            for i in range(2, row)
        ]

        return test_data



        data_value = []
        # for row in ws.iter_rows(min_row=2,values_only=True):
        #     data_value.append(row)
        # return data_value
    except Exception as e:
        print(f"错误信息是：{e}")
        return None

    # 打印所有可用表
    # print(f"所有可用表：{wb.sheetnames}")
    # cell_value = ws['A1'].value
    # print(cell_value)
    # data_item = (for row in ws.iter_rows(min_row=2, values_only=True))


res = excel_read(r'/case_data\case_data.xlsx',
                 'register')
for i in res:
    print(f"这是excel的数据：{i}")
